import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { SheetVO, SheetForm, SheetQuery } from '@/api/sheet/sheet/types';

/**
 * 查询应用列表
 * @param query
 * @returns {*}
 */

export const listSheet = (query?: SheetQuery): AxiosPromise<SheetVO[]> => {
  return request({
    url: '/system/sheet/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询应用详细
 * @param sheetId
 */
export const getSheet = (sheetId: string | number): AxiosPromise<SheetVO> => {
  return request({
    url: '/system/sheet/' + sheetId,
    method: 'get'
  });
};

/**
 * 新增应用
 * @param data
 */
export const addSheet = (data: SheetForm) => {
  return request({
    url: '/system/sheet',
    method: 'post',
    data: data
  });
};

/**
 * 修改应用
 * @param data
 */
export const updateSheet = (data: SheetForm) => {
  return request({
    url: '/system/sheet',
    method: 'put',
    data: data
  });
};

/**
 * 删除应用
 * @param sheetId
 */
export const delSheet = (sheetId: string | number | undefined) => {
  return request({
    url: '/system/sheet/' + sheetId,
    method: 'delete'
  });
};


/**
 * 查询所有应用
 * @param query
 * @returns {*}
 */
export const listSheetAll = (query?: SheetQuery) => {
  return request({
    url: '/system/sheet/listAll',
    method: 'get',
    params: query
  });
};

/**
 * 获取树形应用的名称
 * */
export const findLabelById = (id: number | string, sheetOptions: { sheetId: number | string, name: string, children?: any[] }[]): string | null => {
  for (const option of sheetOptions) {
    if (option.sheetId === id) {
      return option.name;
    }
    if (option.children) {
      const label = findLabelById(id, option.children);
      if (label) {
        return label;
      }
    }
  }
  return null;
};
